#include "RequestRecord.h"

#include <QDebug>
#include <QDateTime>

RequestRecord::RequestRecord()
{

}

RequestRecord::RequestRecord(const QString &id)
{
    auto now = QDateTime::currentDateTime();

    sampleID = id;
    startTime = now.addDays(-1).toString("yyyyMMddHHmmss");
    endTime = now.toString("yyyyMMddHHmmss");
}

QString RequestRecord::buildRecord() const
{
    QString record = recordTypeSymbol(RecordType::Request);
    record += FIELD;
    record += QString::number(frameIndex);
    record += FIELD;
    record += patientID;
    record += FIELD;
    record += sampleID;
    record += FIELD;
    record += startTime;
    record += FIELD;
    record += endTime;
    record += FIELD;
    record += queryCodeList.value(queryCode);
    record += CR;

    return record;
}

bool RequestRecord::analyzeRecord(const QString &str)
{
    QStringList fields = str.split(FIELD);

    if(fields.count() < 7 || fields.value(0) != recordTypeSymbol(RecordType::Request)) {
        // qWarning() << "RequestRecord: Syntax Error!";
        return false;
    }

    patientID = fields[2];
    sampleID = fields[3];
    startTime = fields[4];
    endTime = fields[5];
    if(queryCodeList.indexOf(fields[6]) >= 0)
        queryCode = static_cast<QueryCode>(queryCodeList.indexOf(fields[6]));

    return true;
}
